//动态规划
//profit = dp[i - 1], cost代表i-1天最低价格
//profit = max(profit, dp[i] - min(cost, dp[i]))

function maxProfit(price) {
    let profit = 0
    let cost = Infinity

    for(let i = 0; i < price.length; i++) {
        cost = Math.min(cost, price[i])
        profit = Math.max(profit, price[i] - cost)
    }

    return profit
}

const arr = [7,1,5,3,6,4]
console.log(maxProfit(arr))